package Java9_23;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Products {
    public static void main(String[] args) {
        SAXReader sr=new SAXReader();
        try {
            Document doc=sr.read(new File("D:\\IdeaProjects\\src\\Java9_23\\products.xml"));
//            1：读取products.xml中的商品信息，输出。
            List<Element>list=doc.selectNodes("/shop/product");
//           2： 查找商品信息：根据输入的商品号，输出对应的商品信息。
//            List<Element>list=doc.selectNodes("/shop/product[itemno=2950]");
            for (Element e:list){
                String addr=e.attributeValue("factoryaddr");
                String dis=e.attributeValue("discount");
                Element eitemno= (Element) e.selectSingleNode("itemno");
                String itemno=eitemno.getText();
                Element ename= (Element) e.selectSingleNode("name");
                String name=ename.getText();
                Element eprice= (Element) e.selectSingleNode("price");
                Double price=Double.parseDouble(eprice.getText());
                Element estockcount= (Element) e.selectSingleNode("stockcount");
                String stockcount=estockcount.getText();
                Element eunit= (Element) e.selectSingleNode("unit");
                String unit=eunit.getText();
                System.out.printf("itemno:%s,name:%s,price:%f,stokcount%s,unit:%s\n",itemno,name,price,stockcount,unit);
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
}
